
window.onload=function () {
    if(sessionStorage.getItem("username")!=null) {
        document.getElementById("toLogin").innerHTML = "个人中心";
        document.getElementById("shopNum").style.color = "blue";
        if ($("#toLogin").val() != "登录") {
            $("#toLogin").click(function () {
                location.href = "meCenter.html";
            });
            document.getElementById("register").innerHTML = "退出";
            document.getElementById("register").style.color = "red";
            $("#register").click(function () {
                sessionStorage.clear();
                location.href = "index.html";
            })
        }
    }else
    {
        document.getElementById("toLogin").innerHTML = "登录";
    }
    $("#wellCome").text("你好!"+sessionStorage.getItem("username")+"欢迎回来").css("color","red");
    $.ajax({
        url:"cart/seeCart",
        type:"get",
        datatype:"json",
        data:{
            "userid":sessionStorage.getItem("userid"),
        },
        success:function (res) {
            let count;
            count=res.data.length;
            document.getElementById("shopNum").innerHTML=count;
            document.getElementById("toLogin").style.color="red";
        }
    });
    $.ajax({
        url:"category/selectClassify",
        type:"get",
        datatype:"json",
        data:{
            "id":sessionStorage.getItem("classifyId"),
            "pageNum":pages,
        },
        success:function (res) {
            sessionStorage.setItem("pageAll",res.pages);
            $("#"+sessionStorage.getItem("textId")).css("color","#ccc");
            if(typeof pages == "undefined"){
                pages=1;
            }
            $("#New").empty();
            $("#pages").val(pages);
            $("#total").text("共:"+$("#pages").val()+"/"+res.pages+"页");
            let datas=res.list;
            for(let i=0;i<datas.length;i++) {
                let goods = datas[i];
                let li = $("<li></li>");
                let a = $("<a href='detailed.html?id=" + goods.id + "'>");
                let image = $("<img src='" + goods.image + "'>");
                a.append(image);
                li.append(a);
                let p1 = $("<p></p>").text(goods.name);
                let p2 = $("<p></p>").text(goods.description);
                let p3 = $("<p></p>");
                let span1 = $("<span></span>").text("￥" + goods.salesprice);
                let span2 = $("<span></span>").text("￥" + goods.marketprice);
                li.append(p1);
                li.append(p2);
                p3.append(span1);
                p3.append(span2);
                li.append(p3);
                $("#New").append(li);
            }
        }
    });
};

function toLogin() {
    let uname=$("#uname").val();
    let psd=$("#psd").val();
    $.ajax({
        url:"user/login",
        type:"get",
        data:{
            "uname":uname,
            "psd":psd,
        },
        success:function (res) {
            if(res.code==200){
                sessionStorage.setItem("username",res.data.account);
                sessionStorage.setItem("userid",res.data.id);
                window.location.href="index.html";
            }
            else(
                alert("登陆失败！用户名或密码错误")
            )
        }
    })
}
